package com.zhongwang.mms.module.basic.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.module.basic.service.TWarehouseInfoService;
import com.zhongwang.mms.module.system.log.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @Description: 仓库区位维护 控制层
 * @Author: China.wsg
 * @Date: 2020/05/11 16:23
 */
@Controller
@RequestMapping("/basic/warehouse")
public class TWarehouseInfoController extends BaseController {

    @Autowired
    private TWarehouseInfoService warehouseInfoService;
    @Autowired
    private LogService logService;


    /**
     * 仓库区位 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    @RequiresPermissions({"basic:warehouse:view"})
    public String page() {
        return "basic/warehouse/page";
    }

    /**
     * 仓库区位列表
     *
     * @return 仓库区位 Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"basic:warehouse:view"})
    public List<TWarehouseInfo> list(TWarehouseInfo warehouseInfo) {
        return warehouseInfoService.findWarehouseInfo(warehouseInfo);
    }

    /**
     * 创建仓库
     *
     * @param warehouseInfo 仓库信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"basic:warehouse:edit"})
    public JsonResult save(TWarehouseInfo warehouseInfo) {
        logService.saveLog("新建仓库区位", warehouseInfo.getWhiName());
        return jsonResult(warehouseInfoService.save(warehouseInfo));
    }

    /**
     * 名称唯一验证
     *
     * @param r 仓库
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    public ValidResult checkUnique(TWarehouseInfo r) {
        TWarehouseInfo dept = warehouseInfoService.findWarehouseByName(r.getWhiName());
        if (r.getWhiId() == null) {
            return ValidResult.result(dept);
        }
        if (dept == null || dept.getWhiId().equals(r.getWhiId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 删除部门
     *
     * @param warehouseInfo 部门信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"basic:warehouse:edit"})
    public JsonResult del(TWarehouseInfo warehouseInfo) {
        JsonResult jsonResult = new JsonResult();
        String msg = warehouseInfoService.del(warehouseInfo);
        if (msg.equals("")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 查询仓库
     *
     * @param id 仓库id
     * @return 仓库
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"basic:warehouse:edit"})
    public TWarehouseInfo id(Integer id) {
        return warehouseInfoService.findWarehouseById(id);
    }

    /**
     * 修改仓库
     *
     * @param warehouseInfo 仓库信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"basic:warehouse:edit"})
    public JsonResult update(TWarehouseInfo warehouseInfo) {
        TWarehouseInfo r = warehouseInfoService.findWarehouseById(warehouseInfo.getWhiId());
        logService.saveLog("修改仓库", r, warehouseInfo);
        return jsonResult(warehouseInfoService.updateWarehouse(warehouseInfo));
    }


}
